package com.darren.euc.action;

 import com.darren.euc.entity.*;
 import com.darren.euc.service.TemporaryOrderService;
 import com.darren.euc.util.PropertyHelper;
 import net.sf.json.JSONObject;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Controller;
 import org.springframework.web.bind.annotation.*;

import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by Machenike-Pc on 2018/4/3.
 */
@Controller
@RequestMapping("/temporaryOrder")
public class TemporaryOrderAction {
	
	private static final SimpleDateFormat FORMATTER = new SimpleDateFormat("yyyyMMdd");
	
	
    @Autowired
    private TemporaryOrderService temporaryOrderService;
    /**
     * 查询
     * @param requestJson
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public ResponseJSON queryOrder(@RequestBody JSONObject requestJson, HttpServletRequest request, HttpServletResponse response) {
        ResponseJSON responseJson = new ResponseJSON();
        PageBean pageBean = temporaryOrderService.queryForPageUseHQL(requestJson.getJSONObject("data"), PropertyHelper.getPageSize(), requestJson.getInt("currentPage"));
        responseJson.setData(pageBean);
        return responseJson;
     }

    /**
     * 保存
     * @param order
     * @param request
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    public ResponseJSON saveOrder(@RequestBody TemporaryOrder order, HttpServletRequest request, HttpServletResponse response) {
        //表单提交
        User user = (User) request.getAttribute("user");
        if(user != null){
            order.setCreatedBy(user.getUserName());
            order.setLastModifyBy(user.getUserName());
        }
        TemporaryOrder persistent = null;
        try {
            persistent = temporaryOrderService.saveTempOrder(order);
            persistent.setOrderNumber(generateOrderNumber(temporaryOrderService.getCurrentDayCount(persistent.getId())));
            persistent = temporaryOrderService.saveTempOrder(persistent);            
            if(persistent != null){
                return  ResponseJSON.defaultSuccessResponse();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseJSON().set(-1,e.getMessage(),null);
        }

        return ResponseJSON.defaultFailedResponse();
    }

    private String generateOrderNumber(Integer orderNumber){
		Date currentTime = new Date();
		return "T" + FORMATTER.format(currentTime) + String.format("%03d", orderNumber);
	}
    
    
    /**
     * 详情
     * @param id
     * @param response
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value="/detail")
    public ResponseJSON detail(@RequestParam(name="id") Integer id,HttpServletResponse response,HttpServletRequest request){
        ResponseJSON responseJson = ResponseJSON.defaultSuccessResponse();
        TemporaryOrder detail = temporaryOrderService.findById(id);
        if(detail != null){
            responseJson.setData(detail);
        }else{
            return ResponseJSON.defaultFailedResponse();
        }
        return responseJson;
    }
}
